/**
 * meituan.com Inc.
 * Copyright (c) 2010-2023 All Rights Reserved.
 */
package com.recruit.controller;

/**
 * <p>
 *
 * </p>
 *
 * @author denghufu
 * @version SessionController.java v1.0 2023/3/7 19:32 denghufu Exp $
 */

import com.recruit.mapper.SessionListMapper;
import com.recruit.mapper.UserMapper;
import com.recruit.model.SessionListDO;
import com.recruit.model.UserDO;
import com.recruit.vo.UnifyResponseVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class SessionController {

    @Resource
    private UserMapper userMapper;

    @Resource
    private SessionListMapper seesionListMapper;

    /**
     * 已建立会话
     */
    @GetMapping("/sessionList/already")
    public UnifyResponseVO sessionListAlready(@RequestParam Integer id) {
        List<SessionListDO> sessionLists = seesionListMapper.selectByUserId(id);
        return new UnifyResponseVO(sessionLists);
    }

    /**
     * 可建立会话的用户
     *
     * @param id
     * @return
     */
    @GetMapping("/sessionList/not")
    public UnifyResponseVO sessionListNot(@RequestParam Integer id) {
        List<Integer> list = seesionListMapper.selectUserIdByUserId(id);
        list.add(id);
        List<UserDO> cloudList = userMapper.getCloudList(list);
        return new UnifyResponseVO(cloudList);
    }

    /**
     * 创建会话
     *
     * @param id
     * @param toUserId
     * @param toUserName
     * @return
     */
    @GetMapping("/createSession")
    public UnifyResponseVO createSession(@RequestParam Integer id, @RequestParam Integer toUserId,
                                         @RequestParam String toUserName) {
        SessionListDO sessionList = new SessionListDO();
        sessionList.setUserId(id);
        sessionList.setUnReadCount(0);
        sessionList.setListName(toUserName);
        sessionList.setToUserId(toUserId);
        seesionListMapper.insert(sessionList);
        // 判断对方和我建立会话没有？ 没有也要建立
        Integer integer = seesionListMapper.selectIdByUser(toUserId, id);
        if (integer == null || integer <= 0) {
            UserDO user = userMapper.selectByPrimaryKey(id);
            sessionList.setUserId(toUserId);
            sessionList.setToUserId(id);
            sessionList.setListName(user.getUsername());
            seesionListMapper.insert(sessionList);
        }
        return new UnifyResponseVO();
    }

    /**
     * 删除会话
     *
     * @param sessionId
     * @return
     */
    @GetMapping("/delSession")
    public UnifyResponseVO delSession(@RequestParam Integer sessionId) {
        seesionListMapper.deleteByPrimaryKey(sessionId);
        return new UnifyResponseVO();
    }

}
